package informationretrieval.extensions;

import org.apache.lucene.index.FieldInvertState;
import org.apache.lucene.search.similarities.TFIDFSimilarity;
import org.apache.lucene.util.BytesRef;

/**
 * 
 * MyTFIDFSimilarity.java - an extension for the tf-idf similarity class
 * 
 */
public class MyTFIDFSimilarity extends TFIDFSimilarity {

	/**
	 * A method to calculate a tf value by a given frequency
	 * 
	 * @param frequency
	 *            The term frequency
	 * @return A float value representing the term frequency per a given term
	 */
	public float tf(int frequency) {
		return (float) Math.log(1 + frequency);
	}

	/**
	 * A method to compute the tf value for a term by a given frequency
	 */
	public float tf(float frequency) {
		float termTfValue;
		if (frequency == 0) {
			termTfValue = 0;
		}
		termTfValue = (float) Math.log(1 + frequency);
		return termTfValue;
	}

	/**
	 * A method to get the idf value by the document frequency in a corpus
	 * contains a numDocs parameter
	 */
	public float idf(long docFreq, long numDocs) {
		float termIDFValue = 0;
		if (docFreq == 0) {
			termIDFValue = 0;
		} else {
			double freqTotalDocRelation = (float) numDocs / (float) docFreq;
			double temp = Math.log(freqTotalDocRelation);
			termIDFValue = (float) temp;
		}
		return termIDFValue;
	}

	/**
	 * A method to get the coordination factor return 1 in order to not change
	 * the calculation
	 */
	public float coord(int overlap, int maxOverlap) {
		return 1f;
	}

	/**
	 * A method to get a query normal value return 1 in order to not change the
	 * calculation
	 */
	public float queryNorm(float sumOfSquaredWeights) {
		return 1f;
	}

	/**
	 * A method to get a length normal value return 1 in order to not change the
	 * calculation
	 */
	public float lengthNorm(FieldInvertState state) {
		return 1f;
	}

	/**
	 * A method to decode a normal value return 1 in order to not change the
	 * calculation
	 */
	public float decodeNormValue(long norm) {
		return 1f;
	}

	/** Defualt inherited methods - unused **/

	@Override
	public long encodeNormValue(float arg0) {
		// TODO Auto-generated method stub
		return 0l;
	}

	@Override
	public float scorePayload(int arg0, int arg1, int arg2, BytesRef arg3) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public float sloppyFreq(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

}